﻿mainModule.controller("simpleListCtrl", ["$scope", "$rootScope", "itemSvc", "notification", "pageState", "columnSvc", "$routeParams", "articleSvc", "cache", "config", function ($scope, $rootScope, itemSvc, notification, pageState, columnSvc, $routeParams, articleSvc, cache, config) {
    var articleID = $routeParams.articleID;
    var query;

    $scope.paging = { currentPage: 1, pageSize: config.paging.pageSize, linksSize: config.paging.linksSize };
    $scope.sorting = { column: "ID", ascending: true };
    $scope.showPager = false;    
    $scope.selectedIndex = -1;
    $scope.list = [];
    $scope.showController = false;
                    
    articleSvc.findById(articleID).then(function (response) {            
            if(response && response.data && response.data !== "null")
                $scope.showController = true;            
        });
            
    query = { PageNumber: $scope.paging.currentPage, PageSize: $scope.paging.pageSize, ComputePagesCount: true, SortColumn: $scope.sorting.column, SortAscending: $scope.sorting.ascending };
    query.ArticleID = articleID;
    findBy(query);    

    function findBy(query) {
        $scope.exportQuery = angular.extend(query, { expType: "xls", ComputePagesCount: false });

        itemSvc.simpleFindBy(query).then(function (response) {
                $scope.list = $scope.list.concat(response.data.List);
                $scope.showPager = (response.data.List.length >= config.paging.pageSize);
            });
    }

    function reloadList(filterQuery) {
        query = angular.extend(query, { PageNumber: 1 }, filterQuery);
        $scope.list = [];
        findBy(query);
    }

    var lastEditIndex = -1;
    $scope.edit = function (index) {
        var item;
        if (lastEditIndex >= 0 && lastEditIndex !== index) {            
            item = $scope.list[lastEditIndex];
            if(typeof(item) !== "undefined")
                item.editing = false;
        }

        item = $scope.list[index];
        item.editing = true;
        lastEditIndex = index;        
    }
    
    $scope.save = function (index) {
        var item;
        $scope.savedPressed = true;
        item = $scope.list[index];
        if ($scope.isValidItem(item)) {
            itemSvc.edit(item.ID, item).then(function (response) {
                if (response.data.Success) {
                    item.editing = false;
                    $scope.savedPressed = false;
                }

                notification.notify(response.data);
            });
        }        
    }

    $scope.isValidItem = function (item) {
        if (item && item.Message && item.Message.length > 0 && item.Message.length <= 250)
            return true;
        
        return false;
    }

    $scope.remove = function (index) {
        var item = $scope.list[index];
        itemSvc.remove(item.ID).then(function (response) {
            if (response.data.Success)
                $scope.list.splice(index, 1);                        
            
            notification.notify(response.data);            
        });        
    }

    $scope.loadNextPage = function(pageNumber) {
        $scope.paging.currentPage = $scope.paging.currentPage + 1;
        query = angular.extend(query, { PageNumber: $scope.paging.currentPage, ComputePagesCount: false });
        findBy(query);
    }

    $scope.isFilterVisible = false;
    $scope.toggleFilter = function () {
        $scope.isFilterVisible = !$scope.isFilterVisible;

        if (!$scope.isFilterVisible && query.Message) {
            query.Message = "" ;
            $scope.$emit('listReload', query);
        }
    }

    $scope.select = function (index) {
        $scope.selectedIndex = index;
    }

    $scope.add = function() {
        $scope.$broadcast('itemAdding');
    }

    $scope.$on('listReload', function (e, filterQuery) {
        reloadList(filterQuery);
    });

    $scope.$on('itemAdded', function (e, item) {
        $scope.list.push(item);
    });

    $scope.$on('itemEdited', function (e, item) {
        $scope.list[$scope.selectedIndex] = item;
    });

    $scope.activate = function (index) {
        $scope.activeIndex = index;
    }

    $scope.deactivate = function (index) {
        if ($scope.activeIndex == index)
            $scope.activeIndex = -1;
    }
}]);




